//
//  FroupNotiDetailViewController.m
//  Findif
//
//  Created by Mac on 2019/3/14.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "FroupNotiDetailViewController.h"
#import "GroupInfoViewController.h"
#import "PersonalViewController.h"

@interface FroupNotiDetailViewController ()

@property (nonatomic, weak)UIView * backView;
@property (nonatomic, weak)UIImageView * iconImageView;
@property (nonatomic, weak)UIImageView * rightImageView;
@property (nonatomic, weak)UIImageView * sexImageView;
@property (nonatomic, weak)UILabel * nameLabel;
@property (nonatomic, weak)UILabel * subLabel;
@property (nonatomic, weak)UIView * lineView;
@property (nonatomic, weak)UILabel * contentLabel;
@property (nonatomic, weak)UILabel * gourpLabel;
@property (nonatomic, weak)UILabel * timeLabel;
@property (nonatomic, weak)UIButton * refuseButton;
@property (nonatomic, weak)UIButton * agreeButton;
@property (nonatomic, weak)UILabel * showLabel;

@property (nonatomic, strong)NSDictionary * dict;
@property (nonatomic, copy)NSString * type;

@end

@implementation FroupNotiDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self loadData];
    [self setupNav];
    [self setupUI];
}

- (void)loadData{
    
    [[ZRNetworkTool sharedTool] getGroupInformInfoWithID:self.ID WithBlock:^(BOOL success, id responseData) {
        
        if (success) {
            
//            NSLog(@"res==%@",responseData);
            NSDictionary * dict = responseData[@"data"];
            self.dict = dict;
            NSString * imgStr = [NSString stringWithFormat:@"%@%@",BASEURLSTR,dict[@"group_img"]];
            NSURL * url = [NSURL URLWithString:imgStr];
            [self.iconImageView sd_setImageWithURL:url];
            
            NSArray * timeArr = [dict[@"inputtime"] componentsSeparatedByString:@" "];
            self.timeLabel.text = [NSString stringWithFormat:@"%@",timeArr.firstObject];
            self.type = [NSString stringWithFormat:@"%@",dict[@"inform_type"]];
            int type = [dict[@"inform_type"] intValue];
            
            if (type == 1) {
                
                self.title = @"入团申请";
                
                self.nameLabel.text = dict[@"user_name"];
                self.subLabel.hidden = YES;
                self.sexImageView.hidden = NO;
                int sex = [dict[@"sex"] intValue];
                self.rightImageView.hidden = NO;
                if (sex == 1) {
                    
                    self.sexImageView.image = [UIImage imageNamed:@"男性"];
                }else{
                    
                    self.sexImageView.image = [UIImage imageNamed:@"女性"];
                }
                self.contentLabel.text = [NSString stringWithFormat:@"%@ 申请加入",dict[@"user_name"]];
                self.gourpLabel.text = dict[@"group_name"];
                int status = [dict[@"audit_status"] intValue];
                //入团审核状态  (0为待处理  1为同意  2为拒绝)
                if (status == 0) {
                    
                    self.showLabel.hidden = YES;
                    self.agreeButton.hidden = NO;
                    self.refuseButton.hidden = NO;
                }else if (status == 1) {
                    
                    self.agreeButton.hidden = YES;
                    self.refuseButton.hidden = YES;
                    self.showLabel.hidden = NO;
                    
                    self.showLabel.text = @"已同意该申请";
                }else if (status == 2){
                    
                    self.agreeButton.hidden = YES;
                    self.refuseButton.hidden = YES;
                    self.showLabel.hidden = NO;
                    
                    self.showLabel.text = @"已拒绝该申请";
                }
                
            }else if (type == 2){
                
                self.title = @"社团解散";
                
                self.nameLabel.text = dict[@"group_name"];
                self.subLabel.hidden = NO;
                self.sexImageView.hidden = YES;
                if (dict[@"group_desc"]) {
                    
                  self.subLabel.text = dict[@"group_desc"];
                }else{
                    
                  self.subLabel.text = @"社长什么也没留下";
                }
                
                self.contentLabel.text = [NSString stringWithFormat:@"社长 %@ 已解散 %@",dict[@"user_name"],dict[@"group_name"]];
                self.agreeButton.hidden = YES;
                self.refuseButton.hidden = YES;
                self.showLabel.hidden = YES;
                self.gourpLabel.hidden = YES;
                
            }else if (type == 3){
                
                self.title = @"移出社团";
                
                self.nameLabel.text = dict[@"group_name"];
                self.subLabel.hidden = NO;
                self.sexImageView.hidden = YES;
                if (dict[@"group_desc"]) {
                    
                    self.subLabel.text = dict[@"group_desc"];
                }else{
                    
                    self.subLabel.text = @"社长什么也没留下";
                }
                
                self.contentLabel.text = [NSString stringWithFormat:@"社长 %@ 把你移除",dict[@"user_name"]];
                self.agreeButton.hidden = YES;
                self.refuseButton.hidden = YES;
                self.showLabel.hidden = YES;
                self.gourpLabel.hidden = NO;
                self.gourpLabel.text = dict[@"group_name"];
                
            }else if (type == 4){
                
                self.nameLabel.text = dict[@"group_name"];
                self.subLabel.hidden = NO;
                self.sexImageView.hidden = YES;
                if (dict[@"group_desc"]) {
                    
                    self.subLabel.text = dict[@"group_desc"];
                }else{
                    
                    self.subLabel.text = @"社长什么也没留下";
                }
                self.contentLabel.text = [NSString stringWithFormat:@"社长 %@ 邀请你加入",dict[@"user_name"]];
                self.agreeButton.hidden = YES;
                self.refuseButton.hidden = YES;
                self.showLabel.hidden = YES;
                self.gourpLabel.hidden = NO;
                self.gourpLabel.text = dict[@"group_name"];
            }else if (type == 5){
                
                self.nameLabel.text = dict[@"group_name"];
                self.subLabel.hidden = NO;
                self.sexImageView.hidden = YES;
                if (dict[@"group_desc"]) {
                    
                    self.subLabel.text = dict[@"group_desc"];
                }else{
                    
                    self.subLabel.text = @"社长什么也没留下";
                }
                self.contentLabel.text = [NSString stringWithFormat:@"社长 %@ 拒绝你加入",dict[@"user_name"]];
                self.agreeButton.hidden = YES;
                self.refuseButton.hidden = YES;
                self.showLabel.hidden = YES;
                self.gourpLabel.hidden = NO;
                self.gourpLabel.text = dict[@"group_name"];
            }else{
                
                self.subLabel.text = @"没什么新鲜事~~";
            }
            
            [self.gourpLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.leading.mas_equalTo(self.contentLabel.mas_trailing).offset(5 * SCREENH6);
                make.centerY.mas_equalTo(self.contentLabel);
            }];
        }
    }];
    
    [[ZRNetworkTool sharedTool] updateGroupInformWithID:self.ID WithAudit_status:@"" WithBlock:^(BOOL success, id responseData) {
//        NSLog(@"read===%@",responseData);
        if (success) {
            
//            NSLog(@"read===%@",responseData);
        }else{
            
//            NSLog(@"");
        }
    }];
    
}

- (void)setupNav{
    
    self.title = @"社团通知";
    
}


- (void)setupUI{
    
    self.view.backgroundColor = ColorWithRGB(0xF0F0F0);
    
    UIView * backView = [[UIView alloc] initWithFrame:CGRectMake(15 * SCREENH6, 10 * SCREENH6, 345 * SCREENH6, 100 * SCREENH6)];
    
    backView.userInteractionEnabled = YES;
    backView.backgroundColor = ZRWhiteColor;
    backView.layer.cornerRadius = 5 * SCREENH6;
    backView.layer.masksToBounds = YES;
    //创建轻拍手势
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [backView addGestureRecognizer:tapGR];
    
    self.backView = backView;
    
    [self.view addSubview:self.backView];
    
    
    UIImageView * iconImageView = [UIImageView new];
    
    iconImageView.layer.cornerRadius = 20 * SCREENH6;
    iconImageView.layer.masksToBounds = YES;
    
    self.iconImageView = iconImageView;
    
    [self.backView addSubview:self.iconImageView];
    
    
    UILabel * nameLabel = [UILabel new];
    
    nameLabel.textColor = ZRBlackColor;
    nameLabel.text = @"----";
    nameLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    
    self.nameLabel = nameLabel;
    
    [self.backView addSubview:self.nameLabel];
    
    
    UILabel * subLabel = [UILabel new];
    
    subLabel.textColor = [UIColor lightGrayColor];
    subLabel.text = @"----";
    subLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    
    self.subLabel = subLabel;
    
    [self.backView addSubview:self.subLabel];
    
    
    UIImageView * sexImageView = [UIImageView new];
    
    sexImageView.image = [UIImage imageNamed:@"男性"];

    self.sexImageView = sexImageView;
    
    [self.backView addSubview:self.sexImageView];
    
    
    UIImageView * rightImageView = [UIImageView new];
    
    rightImageView.image = [UIImage imageNamed:@"右箭头"];
    
    self.rightImageView = rightImageView;
    
    [self.backView addSubview:self.rightImageView];
    
    
    UIView * lineView = [UIView new];
    
    lineView.backgroundColor = ColorWithRGB(0xe4e4e4);
    
    self.lineView = lineView;
    
    [self.backView addSubview:self.lineView];
    
    
    UILabel * contentLabel = [UILabel new];
    
    contentLabel.textColor = ZRBlackColor;
    contentLabel.text = @"----";
    contentLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    
    self.contentLabel = contentLabel;
    
    [self.backView addSubview:self.contentLabel];
    
    
    UILabel * gourpLabel = [UILabel new];
    
    gourpLabel.textColor = ColorWithRGB(0x4C87D8);
    gourpLabel.text = @"----";
    gourpLabel.font = [UIFont systemFontOfSize:12 * SCREENH6];
    gourpLabel.userInteractionEnabled = YES;
    //创建轻拍手势
    UITapGestureRecognizer * labelTapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTapAction)];
    [gourpLabel addGestureRecognizer:labelTapGR];
    
    self.gourpLabel = gourpLabel;
    
    [self.backView addSubview:self.gourpLabel];
    
    
    UILabel * timeLabel = [UILabel new];
    
    timeLabel.textColor = [UIColor lightGrayColor];
    timeLabel.text = @"----";
    timeLabel.font = [UIFont systemFontOfSize:13 * SCREENH6];
    timeLabel.textAlignment = NSTextAlignmentRight;
    
    self.timeLabel = timeLabel;
    
    [self.backView addSubview:self.timeLabel];
    
    
    UIButton * refuseButton = [UIButton new];
    
    [refuseButton setTitle:@"拒绝" forState:UIControlStateNormal];
    [refuseButton setTitleColor:ZRWhiteColor forState:UIControlStateNormal];
    [refuseButton addTarget:self action:@selector(clickBottomButton:) forControlEvents:UIControlEventTouchUpInside];
    refuseButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    refuseButton.backgroundColor = ZRBlackColor;
    refuseButton.layer.cornerRadius = 5 * SCREENH6;
    refuseButton.layer.masksToBounds = YES;
    refuseButton.hidden = YES;
    
    self.refuseButton = refuseButton;
    
    [self.view addSubview:self.refuseButton];
    
    
    UIButton * agreeButton = [UIButton new];
    
    [agreeButton setTitle:@"同意" forState:UIControlStateNormal];
    [agreeButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    [agreeButton addTarget:self action:@selector(clickBottomButton:) forControlEvents:UIControlEventTouchUpInside];
    agreeButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    agreeButton.backgroundColor = ColorWithRGB(0xFFE035);
    agreeButton.layer.cornerRadius = 5 * SCREENH6;
    agreeButton.layer.masksToBounds = YES;
    agreeButton.hidden = YES;
    
    self.agreeButton = agreeButton;
    
    [self.view addSubview:self.agreeButton];
    
    
    UILabel * showLabel = [UILabel new];
    
    showLabel.text = @"---";
    showLabel.textColor = [UIColor lightGrayColor];
    showLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    showLabel.textAlignment = NSTextAlignmentCenter;
    
    self.showLabel = showLabel;
    
    [self.view addSubview:self.showLabel];
}


- (void)viewDidLayoutSubviews{
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backView).offset(10 * SCREENH6);
        make.top.mas_equalTo(self.backView).offset(10 * SCREENH6);
        make.width.height.mas_equalTo(40 * SCREENH6);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.iconImageView);
        make.leading.mas_equalTo(self.iconImageView.mas_trailing).offset(10 * SCREENH6);
    }];
    
    [self.sexImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(7 * SCREENH6);
        make.leading.mas_equalTo(self.iconImageView.mas_trailing).offset(10 * SCREENH6);
        make.width.height.mas_equalTo(13 * SCREENH6);
    }];
    
    [self.subLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.nameLabel.mas_bottom).offset(5 * SCREENH6);
        make.leading.mas_equalTo(self.iconImageView.mas_trailing).offset(10 * SCREENH6);
        make.width.mas_equalTo(230 * SCREENH6);
    }];
    
    [self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.backView).offset(-10 * SCREENH6);
        make.top.mas_equalTo(self.backView).offset(25 * SCREENH6);
        make.height.mas_equalTo(12.5 * SCREENH6);
        make.width.mas_equalTo(7 * SCREENH6);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.iconImageView.mas_bottom).offset(10 * SCREENH6);
        make.leading.mas_equalTo(self.backView).offset(10 * SCREENH6);
        make.trailing.mas_equalTo(self.backView).offset(-10 * SCREENH6);
        make.height.mas_equalTo(1 * SCREENH6);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.iconImageView);
        make.top.mas_equalTo(self.lineView.mas_bottom).offset(12 * SCREENH6);
//        make.width.mas_equalTo(200 * SCREENH6);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.backView).offset(-10 * SCREENH6);
        make.centerY.mas_equalTo(self.contentLabel);
    }];
    
    [self.refuseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(25 * SCREENH6);
        make.top.mas_equalTo(self.backView.mas_bottom).offset(20 * SCREENH6);
        make.height.mas_equalTo(30 * SCREENH6);
        make.width.mas_equalTo(154 * SCREENH6);
    }];
    
    [self.agreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.width.height.mas_equalTo(self.refuseButton);
        make.leading.mas_equalTo(self.refuseButton.mas_trailing).offset(15 * SCREENH6);
    }];
    
    [self.showLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.backView.mas_bottom).offset(25 * SCREENH6);
    }];
}



- (void)clickBottomButton:(UIButton *)button{
    //(1为同意 2为拒绝)
    NSString * status;
    if (button == self.refuseButton) {
       
        status = @"2";
    }else{
        
        status = @"1";
    }
    [[ZRNetworkTool sharedTool] updateGroupInformWithID:self.ID WithAudit_status:status WithBlock:^(BOOL success, id responseData) {

        HiddenHUD;
        
        if (success) {
            
            [self loadData];
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseData[@"message"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }];
    
}


- (void)tapAction{
    
    HiddenBar;
    if ([self.type isEqualToString:@"1"]) {
        
        PersonalViewController * person = [PersonalViewController new];
        
        person.ID = self.dict[@"operation_user_id"];
        
        [self.navigationController pushViewController:person animated:YES];
    }else if ([self.type isEqualToString:@"2"]){
        
    }else{
        
        GroupInfoViewController * infoVC = [GroupInfoViewController new];
        
        infoVC.group_id = self.dict[@"group_id"];
        
        [self.navigationController pushViewController:infoVC animated:YES];
    }
}


- (void)labelTapAction{
    
    HiddenBar;
    
    GroupInfoViewController * infoVC = [GroupInfoViewController new];
    
    infoVC.group_id = self.dict[@"group_id"];
    
    [self.navigationController pushViewController:infoVC animated:YES];
    
}

@end
